// pages/detail1/detail1.js
const app = getApp()
Page({
  data: {
    active1: [0],
    iconGood: '/resources/images/cardsPage/icon-good.png',
    avatarBoy: '/resources/images/cardsPage/icon_avatar_boy.png',
    avatarGirl: '/resources/images/cardsPage/icon_avatar_girl.png',
    love:'/resources/images/cardsPage/love.png',
    banner: app.globalData.Banner,
    baseData: app.globalData.BaseData,
    dmData: [],
    symbolLeft: '{{',
    symbolRight: '}}',

  },
  //下拉栏函数
  onChange(event) {
    const {
      key
    } = event.currentTarget.dataset;
    this.setData({
      [key]: event.detail,
    });
  },
  // 处理弹幕位置
  setDM: function (chatList) {
    // 处理弹幕参数
    var dmArr=[];
    var strs = chatList; 

    for (let i = 0; i < strs.length; i++) {
      if (strs[i].length == 0) continue;

      const time = Math.floor(Math.random() * 10);
      const _time = time < 6 ? 6 + i : time + i;
      const top = Math.floor(Math.random() * 80) + 2;
      const _p = {
        content: strs[i],
        top,
        time: _time,
      };
      dmArr.push(_p);
    }
    this.setData({
      chats: dmArr
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    // wx.request({
    //   url: app.globalData.urlHome + '/task/finishTask',
    //   header: {
    //     "accept": "*/*",
    //     "content-type": "application/json",
    //     "Authorization": app.globalData.token
    //   },
    //   data: {
    //     "openid": app.globalData.openid,
    //     "type": 9
    //   },
    //   method: 'POST',
    //   success(r) {
    //     console.log(r.data);
    //   },
    //   fail(e) {
    //   }
    // })
  },
  
  onLoad (options) { 
    var tmp = options.json;
    var json = JSON.parse(this.Decoder(tmp));
    console.log(json);
    this.popularAdd(json);
    this.taskFinish();

    if (json.distance != null && json.distance.length != 0) {
      this.setData({
        pic:json,
        latitude: json.latitude.toFixed(0),
        longitude: json.longitude.toFixed(0),
        distance: json.distance.toFixed(0)
      })
    } else {
      this.setData({
        pic:json,
        latitude: json.latitude.toFixed(0),
        longitude: json.longitude.toFixed(0),
      })
    }
    var that=this;
    wx.request({
      url: app.globalData.urlHome + '/card/getChatListByCardId',
      header: {
        "accept": "*/*",
        "content-type": "application/json",
        "Authorization": app.globalData.token
      },

      data: {
        "cardid":json.cardid
      },
      method: 'GET',

      success(r) {
        var list=[];
        for(var i=0;i<r.data.length;i++){
          list.push(r.data[i].chat);
        }
        that.setDM(list);
        // console.log(r.data);
      },
      fail(e) {

      }
    })

  },

  Decoder(str) {
    var res = "";
    for (var i = 0; i < str.length; i++) {
      if (str[i] == '#') {
        res += '&';
      } else {
        res += str[i];
      }
    }
    return res;
  },

  donateMoney () {
    var that = this
    var latitude = that.data.pic.latitude
    var longitude = that.data.pic.longitude
    var picName = that.data.pic.picName
    wx.request({
      url: app.globalData.urlHome + '/card/addCardsScore',
      header: {
        "accept": "*/*",
        "content-type": "application/json",
        "Authorization": app.globalData.token
      },

      data: {
        "cardid":that.data.pic.cardid,
        "openid": app.globalData.openid,
      },
      method: 'POST',

      success(r) {
        console.log(r.data);
        if(r.statusCode != 200) {
            wx.showToast({
                icon:'error',
                title: '服务器异常',
            });
            return;
        }
        if (r.data == "add score success!") {
          wx.showToast({
            title: '打赏成功！',
          });
        } else if (r.data == "score not enough!") {
          wx.showToast({
            title: '积分不足',
            icon: 'error'
          });
        } else if (r.data == "cannot be self!") {
          wx.showToast({
            title: '不能给自己打赏',
            icon: 'error'
          });
        }
      },

      fail(e) {

      }
    })
  },

  getTalk: function (e) {
    this.setData({
      chat: e.detail.value
    });
  },

  smallTalk () {
    var that = this;
    var json = that.data.pic;

    if (that.data.chat == null || that.data.chat.length == 0) {
      wx.showToast({
        title: '弹幕不能为空！',
        icon: 'error'
      })
      return;
    }

    wx.request({
      url: app.globalData.urlHome + '/card/addCardChat',

      header: {
        "accept": "*/*",
        "content-type": "application/json",
        "Authorization": app.globalData.token
      },

      data: {
        "cardid": that.data.pic.cardid,
        "chat": that.data.chat
      },

      method: 'POST',

      success(r) {
        console.log(r.data);

        if (r.data == "card chatList increased!") {
          wx.showToast({
            title: '发送成功！',
          });
        } else {
          wx.showToast({
            title: '弹幕发送失败！',
            icon: 'error'
          });
        }
        that.danmuFinish();
      },
      fail(e) {
      }
    })
  },

  popularAdd(json) {
    var that = this;
    console.log("========================");
    console.log(json.id);
    wx.request({
      url: app.globalData.urlHome + '/card/cardsPopular',
      header: {
        "accept": "*/*",
        "content-type": "application/json",
        "Authorization": app.globalData.token
      },
      data: {
        "cardid":json.id
      },
      method: 'POST',
      success(r) {
      },
      fail(e) {
      }
    })
  },

  taskFinish() {
    // wx.request({
    //   url: app.globalData.urlHome + '/task/finishTask',
    //   header: {
    //     "accept": "*/*",
    //     "content-type": "application/json",
    //     "Authorization": app.globalData.token
    //   },
    //   data: {
    //     "openid": app.globalData.openid,
    //     "type": 3
    //   },
    //   method: 'POST',
    //   success(r) {
    //     console.log(r.data);
    //   },
    //   fail(e) {
    //   }
    // })
  },

  danmuFinish () {
    wx.request({
      url: app.globalData.urlHome + '/task/finishTask',
      header: {
        "accept": "*/*",
        "content-type": "application/json",
        "Authorization": app.globalData.token
      },

      data: {
        "openid": app.globalData.openid,
        "type": 4
      },
      method: 'POST',

      success(r) {
        console.log(r.data);
      },
      fail(e) {

      }
    })
  },

  goHere: function () {
    var that = this;
    app.globalData.pic = that.data.pic;

    wx.switchTab({
      url: '../Map/map'
    })
  },

  daka: function () {
    var that = this;
    var card = that.data.pic;
    wx.request({
      url: app.globalData.urlHome + '/user/addUserFoot',
      header: {
        "accept": "*/*",
        "content-type": "application/json",
        "Authorization": app.globalData.token
      },

      data: {
        "openid": app.globalData.openid,
        "cardId": card.cardid,
        "longitude": app.globalData.longitude,
        "latitude": app.globalData.latitude
      },
      method: 'POST',

      success(r) {
        console.log(r.data);
        if (r.data == "add foot success!") {
          wx.showToast({
            title: '打卡成功！',
          });
          that.footTask();
        } else if (r.data == "repeated!") {
          wx.showToast({
            title: '你已打卡过啦~',
            icon: 'error'
          });
        } else if (r.data == "distance not enough!") {
          wx.showToast({
            title: '请在附近打卡',
            icon: 'error'
          });
        }

      },
      fail(e) {

      }
    })
  },

  footTask: function () {
    wx.request({
      url: app.globalData.urlHome + '/task/finishTask',
      header: {
        "accept": "*/*",
        "content-type": "application/json",
        "Authorization": app.globalData.token
      },

      data: {
        "openid": app.globalData.openid,
        "type": 7
      },
      method: 'POST',

      success(r) {
        console.log(r.data);
      },
      fail(e) {

      }
    })
  },

  addPlan: function () {
    var plans=app.globalData.plans;
    var pic=this.data.pic;

    for (var i = 0; i < plans.length; i++) {
      if (plans[i].picName==pic.picName) {
        wx.showToast({
          title: '已经添加过了~',
          icon: 'error'
        })
        return;
      }
    }
    plans.push(this.data.pic);
    app.globalData.plans = plans;
    wx.showToast({
      title: '添加成功！',
    })
  },

  ClickedLove: function () {
    var that=this;

    var collect=that.data.pic;
    
    wx.request({
      url: app.globalData.urlHome + '/user/addUserCollect',

      data: {
        "openid": app.globalData.openid,
        "cardId": that.data.pic.cardid
      },
      method: 'POST',
      header: {
        "accept": "*/*",
        "content-type": "application/json",
        "Authorization": app.globalData.token
      },
      success: res => {
        console.log(res.data);
        wx.showToast({
          title:'收藏成功！'
        }) ;
        that.collectFinish();
      },
      fail(e) {

      }
    })
  },

  collectFinish: function () {
    wx.request({
      url: app.globalData.urlHome + '/task/finishTask',
      header: {
        "accept": "*/*",
        "content-type": "application/json",
        "Authorization": app.globalData.token
      },

      data: {
        "openid": app.globalData.openid,
        "type": 8
      },
      method: 'POST',

      success(r) {
        console.log(r.data);
      },
      fail(e) {

      }
    })
  },
})